Mac OS Sound

| Previous | Chapter Contents | Chapter Top | Next |

Scheduling Two Sounds with the Sound Clock

Listing 4 shows an example of scheduling two sounds with the sound clock. It assumes that the SoundHeader of the ScheduledSoundHeader structure has already been established. Note that although each channel has its own clock, both are synchronized with the Mixer. Therefore, obtaining the current time from one channel will result in the same time for the second. This allows both channels to use the same start time.

Listing 9 An example of scheduling two sounds with the sound clock

// turn on the sound clock for two channels

cmd.cmd = clockComponentCmd;
cmd.param1 = true;
err = SndDoImmediate(gChan1, &cmd);
if (err != noErr) return (err);

err = SndDoImmediate(gChan2, &cmd);
if (err != noErr) return (err);

// get the sound clock component from one channel

cmd.cmd = getClockComponentCmd;
cmd.param2 = (long)&clock;
err = SndDoImmediate(gChan1, &cmd);
if (err != noErr) return (err);
// get the current time from one channel

scheduledSound1.startTime.base = nil;
err = ClockGetTime(clock, &scheduledSound1.startTime);
if (err != noErr) return (err);

// add 1/2 second to the current time

deltaTime.value.hi = 0;
deltaTime.value.lo = 1;
deltaTime.scale = 2;
AddTime(&scheduledSound1.startTime, &deltaTime);

// schedule two sounds to play at the same starting time

scheduledSound1.flags = kScheduledSoundDoScheduled;
scheduledSound2.flags = kScheduledSoundDoScheduled;
scheduledSound2.startTime = scheduledSound1.startTime;

cmd.cmd = scheduledSoundCmd;
cmd.param2 = (long)&scheduledSound1;
err = SndDoImmediate(gChan1, &cmd);
if (err != noErr) return (err);

cmd.param2 = (long)&scheduledSound2;
err = SndDoImmediate(gChan2, &cmd);
if (err != noErr) return (err);

A sound channel can only have one scheduled sound at any given time. The Mixer does not queue up additional scheduled sounds. You can stream a sound continuously by starting the first buffer with the scheduledSoundCmd , then use the bufferCmd and callBackCmd sequence with the SndDoCommand function to stream additional samples. This has been the techniqued used in the past. By replacing the first bufferCmd with the new scheduledSoundCmd , you can cause the samples to start playing at a specific point in time.


© 1998 Apple Computer, Inc.

| Previous | Chapter Contents | Chapter Top | Next |